/**
 * @desc 旅游路线列表
 */

import { setState } from './index'
import { query } from '@/utils/http'

function apiGetTravelList(token, params) {
  const str = `
  ($request:RouteQueryRequest!){
    queryTravelRoutes(request:$request){
      page{
        hasNext
      }
      datas{
        rownum
        id
        name
        labelList
        picture
        isVisa
        singlePersonPrice
      }
    }
  }
  `
  return query(token, str, { request: params })
}

const state = {
  keyword: '',
  last: 0,
  hasNext: true,
  list: [],
  initList: []
}

const mutations = { setState }

const actions = {
  // 首页数据
  getInitList({ rootState, commit }) {
    const token = rootState.Client.token
    const params = { page: { pageSize: 10, lastRownum: 0 }, keyword: '' }
    return new Promise((resolve, reject) => {
      apiGetTravelList(token, params)
        .then(res => {
          if (
            (res.data.data && res.data.data.queryTravelRoutes,
            res.data.data.queryTravelRoutes.datas)
          ) {
            commit('setState', { initList: res.data.data.queryTravelRoutes.datas })
          }
          resolve()
        })
        .catch(err => reject(err))
    })
  },

  getList({ rootState, state, commit }) {
    const token = rootState.Client.token
    const params = { page: { pageSize: 10, lastRownum: 0 }, keyword: state.keyword }
    return new Promise((resolve, reject) => {
      apiGetTravelList(token, params)
        .then(res => {
          if (
            (res.data.data && res.data.data.queryTravelRoutes,
            res.data.data.queryTravelRoutes.datas)
          ) {
            const result = res.data.data.queryTravelRoutes
            if (result.datas.length) {
              commit('setState', {
                list: result.datas,
                hasNext: result.page.hasNext,
                last: result.datas.length ? result.datas[result.datas.length - 1].rownum : 0
              })
            } else {
              console.log('empty list')
              commit('setState', {
                list: [],
                hasNext: false,
                last: 0
              })
            }
          }
          resolve()
        })
        .catch(err => reject(err))
    })
  },

  getMore({ rootState, state, commit }) {
    if (!state.hasNext) {
      return Promise.reject('no more data')
    }
    const token = rootState.Client.token
    const params = { page: { pageSize: 30, lastRownum: state.last }, keyword: state.keyword }
    return new Promise((resolve, reject) => {
      apiGetTravelList(token, params)
        .then(res => {
          if (
            res.data.data &&
            res.data.data.queryTravelRoutes &&
            res.data.data.queryTravelRoutes.datas
          ) {
            const result = res.data.data.queryTravelRoutes
            commit('setState', {
              list: [...state.list, ...result.datas],
              hasNext: result.page.hasNext,
              last: result.datas.length ? result.datas[result.datas.length - 1].rownum : 0
            })
            resolve('ok')
          } else {
            reject('err')
          }
        })
        .catch(err => reject(err))
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
